package com.ternnetwork.workflow.config;

import java.io.IOException;

import javax.annotation.PostConstruct;
import javax.sql.DataSource;

import org.activiti.engine.FormService;
import org.activiti.engine.HistoryService;
import org.activiti.engine.IdentityService;
import org.activiti.engine.ManagementService;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngineConfiguration;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.impl.cfg.StandaloneProcessEngineConfiguration;
import org.activiti.engine.repository.DeploymentBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.ResourcePatternResolver;
/**
 * Activiti引擎配置
 * @author xuwenfeng
 *
 */
@Configuration
public class ActivitiConfiguration {

	@Autowired
    private DataSource dataSource;
    @Autowired
    private ResourcePatternResolver resourceLoader;
    
    /**
     * 初始化配置，将创建28张表
     * @return
     */
    @Bean
    public StandaloneProcessEngineConfiguration processEngineConfiguration() {
        StandaloneProcessEngineConfiguration configuration = new StandaloneProcessEngineConfiguration();
        configuration.setDataSource(dataSource);
        configuration.setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE);
        configuration.setAsyncExecutorActivate(false);
        return configuration;
    }
    
    
    
    @Bean
    public ProcessEngine processEngine() {
        return processEngineConfiguration().buildProcessEngine();
    }

    @Bean
    public RepositoryService repositoryService() {
        return processEngine().getRepositoryService();
    }

    @Bean
    public RuntimeService runtimeService() {
        return processEngine().getRuntimeService();
    }

    @Bean
    public TaskService taskService() {
        return processEngine().getTaskService();
    }
    @Bean
    public ManagementService managementService() {
    	 return processEngine().getManagementService();
    }
    
    @Bean
    public IdentityService identityService() {
    	 return processEngine().getIdentityService();
    }
    
    @Bean
    public FormService  formService () {
    	 return processEngine().getFormService();
    }
    @Bean
    public HistoryService historyService() {
    return processEngine().getHistoryService();
    }
    
    
    
    
    
    /**
     * 部署流程
     * 被@PostConstruct修饰的方法会在服务器加载Servlet的时候运行，并且只会被服务器调用一次，类似于Serclet的inti()方法。被@PostConstruct修饰的方法会在构造函数之后，init()方法之前运行。
     * @throws IOException 
     */
    @PostConstruct
    public void initProcess() throws IOException {
    	
    	DeploymentBuilder deploymentBuilder= repositoryService().createDeployment();
        Resource resource = resourceLoader.getResource("classpath:/processes/interview.bpmn");
        deploymentBuilder .enableDuplicateFiltering().addInputStream(resource.getFilename(), resource.getInputStream()).name("xxxx").deploy();
    	
      /* 
        DeploymentBuilder deploymentBuilder= repositoryService().createDeployment();
        Resource resource = resourceLoader.getResource("classpath:/processes/xxxx.bpmn");
        deploymentBuilder .enableDuplicateFiltering().addInputStream(resource.getFilename(), resource.getInputStream()).name("xxxx").deploy();
        deploymentBuilder .enableDuplicateFiltering().addClasspathResource("xxxx.bpmn").name("xxxx").deploy();
        
        */
    }
   
}
